package com.czl.module_storehouse.activity.allocate.home;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.binding.viewadapter.flycoTabLayout.TabEntity;
import com.czl.base.config.AppConstants;
import com.czl.module_base.activity.BaseTabActivity;
import com.czl.module_service.event.AllocateAddEvent;
import com.czl.module_service.event.AllocateHomeEvent;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.bean.CommonStatusBean;
import com.czl.module_storehouse.event.AllocatePostInEvent;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.fragment.AllocateFragment;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_ALLOCATE_HOME)
public class AllocateHomeActivity extends BaseTabActivity {

    private List<SupportFragment> mFragmentList;

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品调拨");
        EventBus.getDefault().register(this);
        //调拨单直接调拨出库
        LiveEventBus.get(AllocateAddEvent.class).observe(this, allocateHomeEvent -> new Handler(Looper.myLooper()).postDelayed(() -> {
            //切换到已调拨tab页
            binding.viewPager2.setCurrentItem(1);
            LiveEventBus.get(AllocateHomeEvent.class).postOrderly(new AllocateHomeEvent());
        }, 200));

        LiveEventBus.get(DetailsPostSuccessEvent.class).observe(this, event -> new Handler(Looper.myLooper()).postDelayed(() -> {
            binding.viewPager2.setCurrentItem(1);
            LiveEventBus.get(AllocateHomeEvent.class).postOrderly(new AllocateHomeEvent());
        }, 200));
    }

    @Override
    protected List<SupportFragment> fragments() {
        if (mFragmentList == null) {
            mFragmentList = new ArrayList<>();
            mFragmentList.add(AllocateFragment.newInstance(0));
            mFragmentList.add(AllocateFragment.newInstance(1));
        }
        return mFragmentList;
    }

    @Override
    protected ArrayList<CustomTabEntity> mTabEntities() {
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        entities.add(new TabEntity("待调拨", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("已调拨", R.mipmap.ic_add, R.mipmap.ic_add));
        return entities;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(AllocatePostInEvent event) {
        if (event != null) {
            CommonStatusBean commonStatusBean = event.getCommonStatusBean();
            if (commonStatusBean == null) {
                new Handler(Looper.myLooper()).postDelayed(() -> {
                    binding.viewPager2.setCurrentItem(1);
                    LiveEventBus.get(AllocateHomeEvent.class).postOrderly(new AllocateHomeEvent());
                }, 200);
            } else if (commonStatusBean.getInStatus() == 2 || commonStatusBean.getStatus() == 2) {
                //全部调出或者全部调入
                binding.viewPager2.setCurrentItem(1);
            } else {
                binding.viewPager2.setCurrentItem(0);
            }
        }
    }
}
